<?php
if ( ! function_exists('resize_avatar'))
{
	function resize_avatar($file, $file_name, $w, $h, $save = false, $crop = false)
	{
		//ob_start();
		require_once 'phpthumb/ThumbLib.inc.php'; 
        $thumb = PhpThumbFactory::create($file);  
		//echo $file_name.$thumb;
		//file_put_contents("acounttemp.txt", ob_get_contents());
		//ob_end_flush(); 
        if($save == true){
			if($crop) $thumb->adaptiveResize($w, $h);
			else $thumb->resize($w, $h);        
        }
	
		$thumb->save($file_name);
	}
}
function v2e_special($value) {
    $value = str_replace("â", "a", $value);
    $value = str_replace("ấ", "a", $value);
    $value = str_replace("ầ", "a", $value);
    $value = str_replace("ẩ", "a", $value);
    $value = str_replace("ẫ", "a", $value);
    $value = str_replace("ậ", "a", $value);
    #---------------------------------A^

    $value = str_replace("Â", "a", $value);
    $value = str_replace("Ấ", "a", $value);
    $value = str_replace("Ầ", "a", $value);
    $value = str_replace("Ẩ", "a", $value);
    $value = str_replace("Ẫ", "a", $value);
    $value = str_replace("Ậ", "a", $value);
    #---------------------------------a

    $value = str_replace("á", "a", $value);
    $value = str_replace("à", "a", $value);
    $value = str_replace("ả", "a", $value);
    $value = str_replace("ã", "a", $value);
    $value = str_replace("ạ", "a", $value);
    #---------------------------------A

    $value = str_replace("Á", "a", $value);
    $value = str_replace("À", "a", $value);
    $value = str_replace("Ả", "a", $value);
    $value = str_replace("Ã", "a", $value);
    $value = str_replace("Ạ", "a", $value);
    #---------------------------------a(

    $value = str_replace("ă", "a", $value);
    $value = str_replace("ắ", "a", $value);
    $value = str_replace("ằ", "a", $value);
    $value = str_replace("ẳ", "a", $value);
    $value = str_replace("ẵ", "a", $value);
    $value = str_replace("ặ", "a", $value);
    #---------------------------------A(

    $value = str_replace("Ă", "a", $value);
    $value = str_replace("Ắ", "a", $value);
    $value = str_replace("Ằ", "a", $value);
    $value = str_replace("Ẳ", "a", $value);
    $value = str_replace("Ẵ", "a", $value);
    $value = str_replace("Ặ", "a", $value);
    #---------------------------------e^

    $value = str_replace("ê", "e", $value);
    $value = str_replace("ế", "e", $value);
    $value = str_replace("ề", "e", $value);
    $value = str_replace("ể", "e", $value);
    $value = str_replace("ễ", "e", $value);
    $value = str_replace("ệ", "e", $value);
    #---------------------------------E^

    $value = str_replace("Ê", "e", $value);
    $value = str_replace("Ế", "e", $value);
    $value = str_replace("Ề", "e", $value);
    $value = str_replace("Ể", "e", $value);
    $value = str_replace("Ễ", "e", $value);
    $value = str_replace("Ệ", "e", $value);
    #---------------------------------e

    $value = str_replace("é", "e", $value);
    $value = str_replace("è", "e", $value);
    $value = str_replace("ẻ", "e", $value);
    $value = str_replace("ẽ", "e", $value);
    $value = str_replace("ẹ", "e", $value);
    #---------------------------------E

    $value = str_replace("É", "e", $value);
    $value = str_replace("È", "e", $value);
    $value = str_replace("Ẻ", "e", $value);
    $value = str_replace("Ẽ", "e", $value);
    $value = str_replace("Ẹ", "e", $value);
    #---------------------------------i

    $value = str_replace("í", "i", $value);
    $value = str_replace("ì", "i", $value);
    $value = str_replace("ỉ", "i", $value);
    $value = str_replace("ĩ", "i", $value);
    $value = str_replace("ị", "i", $value);
    #---------------------------------I

    $value = str_replace("Í", "i", $value);
    $value = str_replace("Ì", "i", $value);
    $value = str_replace("Ỉ", "i", $value);
    $value = str_replace("Ĩ", "i", $value);
    $value = str_replace("Ị", "i", $value);
    #---------------------------------o^

    $value = str_replace("ô", "o", $value);
    $value = str_replace("ố", "o", $value);
    $value = str_replace("ồ", "o", $value);
    $value = str_replace("ổ", "o", $value);
    $value = str_replace("ỗ", "o", $value);
    $value = str_replace("ộ", "o", $value);
    #---------------------------------O^

    $value = str_replace("Ô", "o", $value);
    $value = str_replace("Ố", "o", $value);
    $value = str_replace("Ồ", "o", $value);
    $value = str_replace("Ổ", "o", $value);
    $value = str_replace("Ỗ", "o", $value);
    $value = str_replace("Ộ", "o", $value);
    #---------------------------------o*

    $value = str_replace("ơ", "o", $value);
    $value = str_replace("ớ", "o", $value);
    $value = str_replace("ờ", "o", $value);
    $value = str_replace("ở", "o", $value);
    $value = str_replace("ỡ", "o", $value);
    $value = str_replace("ợ", "o", $value);
    #---------------------------------O*

    $value = str_replace("Ơ", "o", $value);
    $value = str_replace("Ớ", "o", $value);
    $value = str_replace("Ờ", "o", $value);
    $value = str_replace("Ở", "o", $value);
    $value = str_replace("Ỡ", "o", $value);
    $value = str_replace("Ợ", "o", $value);
    #---------------------------------u*

    $value = str_replace("ư", "u", $value);
    $value = str_replace("ứ", "u", $value);
    $value = str_replace("ừ", "u", $value);
    $value = str_replace("ử", "u", $value);
    $value = str_replace("ữ", "u", $value);
    $value = str_replace("ự", "u", $value);
    #---------------------------------U*

    $value = str_replace("Ư", "u", $value);
    $value = str_replace("Ứ", "u", $value);
    $value = str_replace("Ừ", "u", $value);
    $value = str_replace("Ử", "u", $value);
    $value = str_replace("Ữ", "u", $value);
    $value = str_replace("Ự", "u", $value);
    #---------------------------------y

    $value = str_replace("ý", "y", $value);
    $value = str_replace("ỳ", "y", $value);
    $value = str_replace("ỷ", "y", $value);
    $value = str_replace("ỹ", "y", $value);
    $value = str_replace("ỵ", "y", $value);
    #---------------------------------Y

    $value = str_replace("Ý", "y", $value);
    $value = str_replace("Ỳ", "y", $value);
    $value = str_replace("Ỷ", "y", $value);
    $value = str_replace("Ỹ", "y", $value);
    $value = str_replace("Ỵ", "y", $value);
    #---------------------------------DD

    $value = str_replace("Đ", "d", $value);
    $value = str_replace("đ", "d", $value);
    #---------------------------------o

    $value = str_replace("ó", "o", $value);
    $value = str_replace("ò", "o", $value);
    $value = str_replace("ỏ", "o", $value);
    $value = str_replace("õ", "o", $value);
    $value = str_replace("ọ", "o", $value);
    #---------------------------------O

    $value = str_replace("Ó", "o", $value);
    $value = str_replace("Ò", "o", $value);
    $value = str_replace("Ỏ", "o", $value);
    $value = str_replace("Õ", "o", $value);
    $value = str_replace("Ọ", "o", $value);
    #---------------------------------u

    $value = str_replace("ú", "u", $value);
    $value = str_replace("ù", "u", $value);
    $value = str_replace("ủ", "u", $value);
    $value = str_replace("ũ", "u", $value);
    $value = str_replace("ụ", "u", $value);
    #---------------------------------U

    $value = str_replace("Ú", "u", $value);
    $value = str_replace("Ù", "u", $value);
    $value = str_replace("Ủ", "u", $value);
    $value = str_replace("Ũ", "u", $value);
    $value = str_replace("Ụ", "u", $value);
    #---------------------------------

    return $value;
}

function v2e($value) {
    #---------------------------------SPECIAL	
    $value = str_replace("&quot;", "", $value);
    $value = str_replace(".", "", $value);
    $value = str_replace("=", "", $value);
    $value = str_replace("+", "", $value);
    $value = str_replace("!", "", $value);
    $value = str_replace("@", "", $value);
    $value = str_replace("#", "", $value);
    $value = str_replace("$", "", $value);
    $value = str_replace("%", "", $value);
    $value = str_replace("^", "", $value);
    $value = str_replace("&", "", $value);
    $value = str_replace("*", "", $value);
    $value = str_replace("(", "", $value);
    $value = str_replace(")", "", $value);
    $value = str_replace("`", "", $value);
    $value = str_replace("~", "", $value);
    $value = str_replace(",", "", $value);
    $value = str_replace("/", "", $value);
    $value = str_replace("\\", "", $value);
    $value = str_replace('"', "", $value);
    $value = str_replace("'", "", $value);
    $value = str_replace(":", "", $value);
    $value = str_replace(";", "", $value);
    $value = str_replace("|", "", $value);
    $value = str_replace("[", "", $value);
    $value = str_replace("]", "", $value);
    $value = str_replace("{", "", $value);
    $value = str_replace("}", "", $value);
    $value = str_replace("(", "", $value);
    $value = str_replace(")", "", $value);
    $value = str_replace("?", "", $value);
    #---------------------------------a^

    $value = str_replace("â", "a", $value);
    $value = str_replace("ấ", "a", $value);
    $value = str_replace("ầ", "a", $value);
    $value = str_replace("ẩ", "a", $value);
    $value = str_replace("ẫ", "a", $value);
    $value = str_replace("ậ", "a", $value);
    #---------------------------------A^

    $value = str_replace("Â", "a", $value);
    $value = str_replace("Ấ", "a", $value);
    $value = str_replace("Ầ", "a", $value);
    $value = str_replace("Ẩ", "a", $value);
    $value = str_replace("Ẫ", "a", $value);
    $value = str_replace("Ậ", "a", $value);
    #---------------------------------a

    $value = str_replace("á", "a", $value);
    $value = str_replace("à", "a", $value);
    $value = str_replace("ả", "a", $value);
    $value = str_replace("ã", "a", $value);
    $value = str_replace("ạ", "a", $value);
    #---------------------------------A

    $value = str_replace("Á", "a", $value);
    $value = str_replace("À", "a", $value);
    $value = str_replace("Ả", "a", $value);
    $value = str_replace("Ã", "a", $value);
    $value = str_replace("Ạ", "a", $value);
    #---------------------------------a(

    $value = str_replace("ă", "a", $value);
    $value = str_replace("ắ", "a", $value);
    $value = str_replace("ằ", "a", $value);
    $value = str_replace("ẳ", "a", $value);
    $value = str_replace("ẵ", "a", $value);
    $value = str_replace("ặ", "a", $value);
    #---------------------------------A(

    $value = str_replace("Ă", "a", $value);
    $value = str_replace("Ắ", "a", $value);
    $value = str_replace("Ằ", "a", $value);
    $value = str_replace("Ẳ", "a", $value);
    $value = str_replace("Ẵ", "a", $value);
    $value = str_replace("Ặ", "a", $value);
    $value = str_replace("Ă", "a", $value);
    #---------------------------------e^

    $value = str_replace("ê", "e", $value);
    $value = str_replace("ế", "e", $value);
    $value = str_replace("ề", "e", $value);
    $value = str_replace("ể", "e", $value);
    $value = str_replace("ễ", "e", $value);
    $value = str_replace("ệ", "e", $value);
    #---------------------------------E^

    $value = str_replace("Ê", "e", $value);
    $value = str_replace("Ế", "e", $value);
    $value = str_replace("Ề", "e", $value);
    $value = str_replace("Ể", "e", $value);
    $value = str_replace("Ễ", "e", $value);
    $value = str_replace("Ệ", "e", $value);
    #---------------------------------e

    $value = str_replace("é", "e", $value);
    $value = str_replace("è", "e", $value);
    $value = str_replace("ẻ", "e", $value);
    $value = str_replace("ẽ", "e", $value);
    $value = str_replace("ẹ", "e", $value);
    #---------------------------------E

    $value = str_replace("É", "e", $value);
    $value = str_replace("È", "e", $value);
    $value = str_replace("Ẻ", "e", $value);
    $value = str_replace("Ẽ", "e", $value);
    $value = str_replace("Ẹ", "e", $value);
    #---------------------------------i

    $value = str_replace("í", "i", $value);
    $value = str_replace("ì", "i", $value);
    $value = str_replace("ỉ", "i", $value);
    $value = str_replace("ĩ", "i", $value);
    $value = str_replace("ị", "i", $value);
    #---------------------------------I

    $value = str_replace("Í", "i", $value);
    $value = str_replace("Í", "i", $value);
    $value = str_replace("Ỉ", "i", $value);
    $value = str_replace("Ĩ", "i", $value);
    $value = str_replace("Ị", "i", $value);
    #---------------------------------o^

    $value = str_replace("ô", "o", $value);
    $value = str_replace("ố", "o", $value);
    $value = str_replace("ồ", "o", $value);
    $value = str_replace("ổ", "o", $value);
    $value = str_replace("ỗ", "o", $value);
    $value = str_replace("ộ", "o", $value);
    #---------------------------------O^

    $value = str_replace("Ô", "o", $value);
    $value = str_replace("Ố", "o", $value);
    $value = str_replace("Ồ", "o", $value);
    $value = str_replace("Ổ", "o", $value);
    $value = str_replace("Ỗ", "o", $value);
    $value = str_replace("Ộ", "o", $value);
    #---------------------------------o*

    $value = str_replace("ơ", "o", $value);
    $value = str_replace("ớ", "o", $value);
    $value = str_replace("ờ", "o", $value);
    $value = str_replace("ở", "o", $value);
    $value = str_replace("ỡ", "o", $value);
    $value = str_replace("ợ", "o", $value);
    #---------------------------------O*

    $value = str_replace("Ơ", "o", $value);
    $value = str_replace("Ớ", "o", $value);
    $value = str_replace("Ờ", "o", $value);
    $value = str_replace("Ở", "o", $value);
    $value = str_replace("Ỡ", "o", $value);
    $value = str_replace("Ợ", "o", $value);
    #---------------------------------u*

    $value = str_replace("ư", "u", $value);
    $value = str_replace("ứ", "u", $value);
    $value = str_replace("ừ", "u", $value);
    $value = str_replace("ử", "u", $value);
    $value = str_replace("ữ", "u", $value);
    $value = str_replace("ự", "u", $value);
    #---------------------------------U*

    $value = str_replace("Ư", "u", $value);
    $value = str_replace("Ứ", "u", $value);
    $value = str_replace("Ừ", "u", $value);
    $value = str_replace("Ử", "u", $value);
    $value = str_replace("Ữ", "u", $value);
    $value = str_replace("Ự", "u", $value);
    #---------------------------------y

    $value = str_replace("ý", "y", $value);
    $value = str_replace("ỳ", "y", $value);
    $value = str_replace("ỷ", "y", $value);
    $value = str_replace("ỹ", "y", $value);
    $value = str_replace("ỵ", "y", $value);
    #---------------------------------Y

    $value = str_replace("Ý", "y", $value);
    $value = str_replace("Ỳ", "y", $value);
    $value = str_replace("Ỷ", "y", $value);
    $value = str_replace("Ỹ", "y", $value);
    $value = str_replace("Ỵ", "y", $value);
    #---------------------------------DD

    $value = str_replace("Đ", "d", $value);
    $value = str_replace("đ", "d", $value);
    #---------------------------------o

    $value = str_replace("ó", "o", $value);
    $value = str_replace("ò", "o", $value);
    $value = str_replace("ỏ", "o", $value);
    $value = str_replace("õ", "o", $value);
    $value = str_replace("ọ", "o", $value);
    #---------------------------------O

    $value = str_replace("Ó", "o", $value);
    $value = str_replace("Ò", "o", $value);
    $value = str_replace("Ỏ", "o", $value);
    $value = str_replace("Õ", "o", $value);
    $value = str_replace("Ọ", "o", $value);
    #---------------------------------u

    $value = str_replace("ú", "u", $value);
    $value = str_replace("ù", "u", $value);
    $value = str_replace("ủ", "u", $value);
    $value = str_replace("ũ", "u", $value);
    $value = str_replace("ụ", "u", $value);
    #---------------------------------U

    $value = str_replace("Ú", "u", $value);
    $value = str_replace("Ù", "u", $value);
    $value = str_replace("Ủ", "u", $value);
    $value = str_replace("Ũ", "u", $value);
    $value = str_replace("Ụ", "u", $value);
    #---------------------------------

    return $value;
}

if (!function_exists('SEO')) {

    function SEO($name = '') {
        $name = v2e($name);
        $name = preg_replace("/[^a-z,A-Z,0-9,_,-]/", "-", $name);
        $name = str_replace("---", "-", $name);
        $name = str_replace("--", "-", $name);
        return strtolower($name);
    }

}

if (!function_exists('random_string')) {

    function random_string($length = 9) {
        $sWord = '';
        $sChars = 'abcdefghjklmnprtwyzABCDEFGHJKLMNPRTWXYZ1234567890';
        for ($i = 1; $i <= $length; $i++) {
            $nNumber = rand(1, strlen($sChars));
            $sWord .= substr($sChars, $nNumber - 1, 1);
        }
        return $sWord;
    }

}

if (!function_exists('pr')) {

    function pr($data, $type = 0) {
        print '<pre>';
        print_r($data);
        print '</pre>';
        if ($type != 0) {
            exit();
        }
    }

}

if (!function_exists('CutText')) {

    function CutText($text, $n = 80) {
        // string is shorter than n, return as is
        if (strlen($text) <= $n) {
            return $text;
        }
        $text = substr($text, 0, $n);
        if ($text[$n - 1] == ' ') {
            return trim($text) . "...";
        }
        $x = explode(" ", $text);
        $sz = sizeof($x);
        if ($sz <= 1) {
            return $text . "...";
        }
        $x[$sz - 1] = '';
        return trim(implode(" ", $x)) . "...";
    }

}

function getIP() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {  //check ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    //to check ip is pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

function resizeImage($src, $w = 0, $h = 0) {
    if ($w != 0 && $h != 0) {
        return PATH_URL . 'imgresize.php?width=' . $w . '&height=' . $h . '&cropratio=1:1&image=' . $src;
    } elseif ($w != 0) {
        return PATH_URL . 'imgresize.php?width=' . $w . '&image=' . $src;
    } elseif ($h != 0) {
        return PATH_URL . 'imgresize.php?height=' . $h . '&image=' . $src;
    }
}

function check_dir_upload($targetDir) {
    if (!is_dir($targetDir . date('Y'))) {
        @mkdir($targetDir . date('Y'), 0777);
    }
    if (!is_dir($targetDir . date('Y') . '/' . date('m'))) {
        @mkdir($targetDir . date('Y') . '/' . date('m'), 0777);
    }
    if (!is_dir($targetDir . date('Y') . '/' . date('m') . '/' . date('d'))) {
        @mkdir($targetDir . date('Y') . '/' . date('m') . '/' . date('d'), 0777);
    }
}

function parserEditorHTML($str) {
    $str = str_replace('../../../static/uploads/editor/', PATH_URL . 'static/uploads/editor/', $str);
    $str = preg_replace('/http:\/\/www.youtube.com\/watch\?v=([A-Za-z0-9\-\_]+)&amp;feature=([A-Za-z0-9]+)/is', '<iframe width="500" height="289" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $str);
    $str = preg_replace('/http:\/\/www.youtube.com\/watch\?v=([A-Za-z0-9\-\_]+)/is', '<iframe width="500" height="289" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $str);
    return $str;
}

function url_login_openid($type = 'google') {
    if ($type == 'google') {
        $params = array(
            'response_type' => 'token',
            'client_id' => G_CLIENT_ID,
            'redirect_uri' => REDIRECT_URL.'/google',
            'state' => 'profile',
            'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.google.com/m8/feeds'
        );

        $url = 'https://accounts.google.com/o/oauth2/auth?';
        foreach ($params as $key => $param) {
            $url .= $key . '=' . urlencode($param) . '&';
        }
    }
    if ($type == 'yahoo') {
        $params = array(
            'openid.claimed_id' => 'http://specs.openid.net/auth/2.0/identifier_select',
            'openid.identity' => 'http://specs.openid.net/auth/2.0/identifier_select',
            'openid.mode' => 'checkid_setup',
            'openid.ns' => 'http://specs.openid.net/auth/2.0',
            'openid.realm' => PATH_URL,
            'openid.return_to' => REDIRECT_URL . '/yahoo',
            'openid.ns.oauth' => 'http://specs.openid.net/extensions/oauth/1.0',
            'openid.oauth.consumer' => YH_CLIENT_KEY,
            'openid.ns.ax' => 'http://openid.net/srv/ax/1.0',
            'openid.ax.mode' => 'fetch_request',
            'openid.ax.required' => 'email,fullname,nickname,gender,language,timezone,image',
            'openid.ax.type.email' => 'http://axschema.org/contact/email',
            'openid.ax.type.fullname' => 'http://axschema.org/namePerson',
            'openid.ax.type.nickname' => 'http://axschema.org/namePerson/friendly',
            'openid.ax.type.gender' => 'http://axschema.org/person/gender',
            'openid.ax.type.image' => 'http://axschema.org/media/image/default',
        );

        $url = 'https://open.login.yahooapis.com/openid/op/auth?';
        foreach ($params as $key => $param) {
            $url .= $key . '=' . urlencode($param) . '&';
        }
    }
    if ($type == 'fb') {
        $url = 'https://www.facebook.com/dialog/oauth/?client_id=' . FB_CLIENT_ID . '&redirect_uri=' . REDIRECT_URL . '&state=' . FB_CLIENT_SECRET . '&display=popup&scope=email,publish_stream';
    }
    return $url;
}

function get_attr_profile_openid($type) {
    include('application/libraries/eac_curl.class.php');
    $info = array();
    if ($type == 'google') {
        $options = array();
        $options['CURLOPT_AUTOREFERER'] = 1;
        $options['CURLOPT_CRLF'] = 1;
        $options['CURLOPT_NOPROGRESS'] = 1;
        $http = new cURL($options);
        $http->setOptions($options);
        $src = $http->get("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" . $_GET['access_token']);
        $contact = $http->get("https://www.google.com/m8/feeds/contacts/default/full?max-results=5000&oauth_token=" . $_GET['access_token']);
        $xml = new SimpleXMLElement($contact);
        $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
        $result = $xml->xpath('//gd:email');

        $profile = json_decode($src);
        $info['type'] = 'google';
        if (isset($profile->id)) {
            $info['id'] = $profile->id;
        } else {
            $info['id'] = '';
        }
        if ($profile->email) {
            $info['email'] = $profile->email;
        } else {
            $info['email'] = '';
        }
        if ($profile->verified_email) {
            $info['verified_email'] = $profile->verified_email;
        } else {
            $info['verified_email'] = '';
        }
        if ($profile->name) {
            $info['full_name'] = $profile->name;
        } else {
            $info['full_name'] = '';
        }
        if ($profile->given_name) {
            $info['f_name'] = $profile->given_name;
        } else {
            $info['f_name'] = '';
        }
        if ($profile->family_name) {
            $info['l_name'] = $profile->family_name;
        } else {
            $info['l_name'] = '';
        }
        if (isset($profile->picture)) {
            $info['avatar'] = $profile->picture;
        } else {
            $info['avatar'] = '';
        }
        if (isset($profile->locale)) {
            $info['locale'] = $profile->locale;
        } else {
            $info['locale'] = '';
        }
        if (isset($profile->timezone)) {
            $info['timezone'] = $profile->timezone;
        } else {
            $info['timezone'] = '';
        }
        if (isset($profile->gender)) {
            $info['gender'] = $profile->gender;
        } else {
            $info['gender'] = '';
        }
        foreach ($result as $title) {
            $info['contact'][] = $title->attributes()->address;
        }
    } elseif ($type == 'yahoo') {
        $info['type'] = 'yahoo';
        if (isset($_REQUEST['openid_ax_value_email'])) {
            $info['email'] = $_REQUEST['openid_ax_value_email'];
        } else {
            $info['email'] = '';
        }

        if (isset($_REQUEST['openid_ax_value_fullname'])) {
            $info['full_name'] = $_REQUEST['openid_ax_value_fullname'];
        } else {
            $info['full_name'] = '';
        }

        if (isset($_REQUEST['openid_ax_value_nickname'])) {
            $info['nick_name'] = $_REQUEST['openid_ax_value_nickname'];
        } else {
            $info['nick_name'] = '';
        }

        if (isset($_REQUEST['openid_ax_value_gender'])) {
            $info['gender'] = $_REQUEST['openid_ax_value_gender'];
        } else {
            $info['gender'] = '';
        }

        if (isset($_REQUEST['openid_ax_value_image'])) {
            $info['avatar'] = $_REQUEST['openid_ax_value_image'];
        } else {
            $info['avatar'] = '';
        }
    } else {
        if (!empty($_GET['access_token'])) {
            $options = array();
            $options['CURLOPT_AUTOREFERER'] = 1;
            $options['CURLOPT_CRLF'] = 1;
            $options['CURLOPT_NOPROGRESS'] = 1;
            $http = new cURL($options);
            $http->setOptions($options);
            $src = $http->get("https://graph.facebook.com/me?access_token=" . $_GET['access_token']);
            $profile = json_decode($src);
            if (isset($profile->id)) {
                $info['id'] = $profile->id;
            }
            if ($profile->email) {
                $info['email'] = $profile->email;
            }
            if ($profile->name) {
                $info['full_name'] = $profile->name;
            }
            $info['access_token'] = $_GET['access_token'];
        }
    }
    return $info;
}

if (!function_exists('getWeek')) {

    function getWeek() {
        $current_week = 1;
        $current_date = mktime(date('H'), date('i'), date('s'), date('m'), date('d'), date('Y'));
        $week_1_start = mktime('00', '00', '00', '11', '01', '2012');
        $week_1_end = mktime('23', '59', '59', '11', '07', '2012');
        $week_2_start = mktime('00', '00', '00', '11', '08', '2012');
        $week_2_end = mktime('23', '59', '59', '11', '14', '2012');
        $week_3_start = mktime('00', '00', '00', '11', '15', '2012');
        $week_3_end = mktime('23', '59', '59', '11', '21', '2012');
        $week_4_start = mktime('00', '00', '00', '11', '22', '2012');
        $week_4_end = mktime('23', '59', '59', '11', '30', '2012');
        $week_5_start = mktime('00', '00', '00', '11', '22', '2012');
        $week_5_end = mktime('23', '59', '59', '11', '30', '2012');
        $week_6_start = mktime('00', '00', '00', '11', '22', '2012');
        $week_6_end = mktime('23', '59', '59', '11', '30', '2012');
        $week_7_start = mktime('00', '00', '00', '11', '22', '2012');
        $week_7_end = mktime('23', '59', '59', '11', '30', '2012');
        $week_8_start = mktime('00', '00', '00', '11', '22', '2012');
        $week_8_end = mktime('23', '59', '59', '11', '30', '2012');

        if ($current_date >= $week_1_start && $current_date <= $week_1_end) {
            $current_week = 1;
        } elseif ($current_date >= $week_2_start && $current_date <= $week_2_end) {
            $current_week = 2;
        } elseif ($current_date >= $week_3_start && $current_date <= $week_3_end) {
            $current_week = 3;
        } elseif ($current_date >= $week_4_start && $current_date <= $week_4_end) {
            $current_week = 4;
        } elseif ($current_date >= $week_5_start && $current_date <= $week_5_end) {
            $current_week = 5;
        } elseif ($current_date >= $week_6_start && $current_date <= $week_6_end) {
            $current_week = 6;
        } elseif ($current_date >= $week_7_start && $current_date <= $week_7_end) {
            $current_week = 7;
        } elseif ($current_date >= $week_8_start && $current_date <= $week_8_end) {
            $current_week = 8;
        } else {
            $current_week = 1;
        }

        return $current_week;
    }

}


if (!function_exists('last_query')) {

    function last_query($exit = false) {
        $CI = & get_instance();
        echo $CI->db->last_query();
        if ($exit) {
            exit();
        }
    }

}

if (!function_exists('is_file')) {

    function is_file($link_file) {
        $img_temp = null;
        if (file_exists($link_file)) {
            $img_temp = $link_file;
        } else {
            $img_temp = 'default.jpg';
        }
        return $img_temp;
    }

}

if (!function_exists('validate_length')) {

    function validate_length($string, $min = NULL, $max = NULL, $is_number = true) {
        $str_length = strlen($string);
        if ($min != NULL && $max != NULL) {
            if ($str_length >= $min && $str_length <= $max) {
                return true;
            } else {
                return false;
            }
        } elseif ($min != NULL) {
            if ($str_length >= $min) {
                return true;
            } else {
                return false;
            }
        } else {
            if ($str_length <= $max) {
                return true;
            } else {
                return false;
            }
        }
    }

}

/* * ****
 * Kiem tra du lieu email
 * Return : Boolean
 */
if (!function_exists('validate_email')) {

    function validate_email($email) {
        return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) ? FALSE : TRUE;
    }

}

if (!function_exists('contest_upload_image')) {

    function contest_upload_image($file = '', $dir_file, $width, $height) {
        $CI = & get_instance();
        $CI->load->library('image_lib');

        $config_thumb_fb = array();
        $config_thumb_fb['image_library'] = 'gd2';
        $config_thumb_fb['source_image'] = DIR_CONTEST . $file;
        $config_thumb_fb['new_image'] = $dir_file . $file;
        $config_thumb_fb['create_thumb'] = FALSE;
        $config_thumb_fb['maintain_ratio'] = TRUE;
        $config_thumb_fb['width'] = $width;
        $config_thumb_fb['height'] = $height;
        $this->image_lib->initialize($config_thumb_fb);
        $this->image_lib->resize();
        $this->image_lib->clear();
    }

}
if (!function_exists('header_xml')) {

    function header_xml() {
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Content-type: text/xml; charset=utf-8');
        echo '<?xml version="1.0" encoding="utf-8" ?>';
        echo '<data>';
    }

}
if (!function_exists('footer_xml')) {

    function footer_xml() {
        echo '</data>';
    }

}

function getTypeContest($type) {
    $style = '';
    switch ($type) {
        case '1':
            $style = "Image";
            break;
        case '2':
            $style = "Video";
            break;
    }
    return $style;
}

if (!function_exists('get_link_img')) {

    function get_link_img($img = '', $width = 0, $height = 0) {
        $html = PATH_URL . 'static/images/tim/timthumb.php?src=' . $img . '&w=' . $width . '&h=' . $height;
        return $html;
    }

}
if (!function_exists('getVNDateTime')) {

    function getVNDateTime($datetime, $gio = false) {
        $date = '';
        if (!empty($datetime)) {
            if ($gio == true) {
                $date = date('d/m/Y H:i:s', strtotime($datetime));
            } else {
                $date = date('d/m/Y', strtotime($datetime));
            }
        }
        return $date;
    }

}

if (!function_exists('img')) {

    function img($image_path, $width = 0, $height = 0) {
        //Get the Codeigniter object by reference
        $CI = & get_instance();
        //Alternative image if file was not found
        if (!file_exists($image_path) || !is_file($image_path)) {
            $image_path = 'static/images/img/no-image.jpg';
        }

        //The new generated filename we want
        $fileinfo = pathinfo($image_path);

        //MAKE A FOLDER
        if (!empty($width) && !empty($height)) {
            $uploadDir_Thumb = $fileinfo['dirname'] . '/' . $width . 'x' . $height . '/';
        } else {
            $uploadDir_Thumb = $fileinfo['dirname'] . '/thumb/';
        }
        Newfolder($uploadDir_Thumb);
        $new_image_path = $uploadDir_Thumb . $fileinfo['filename'] . '.' . $fileinfo['extension'];

        //The first time the image is requested
        //Or the original image is newer than our cache image
        if ((!file_exists($new_image_path)) || filemtime($new_image_path) < filemtime($image_path)) {
            $CI->load->library('image_lib');
            //The original sizes
            $original_size = getimagesize($image_path);
            $original_width = $original_size[0];
            $original_height = $original_size[1];
            $ratio = $original_width / $original_height;

            //The requested sizes
            $requested_width = $width;
            $requested_height = $height;

            //Initialising
            $new_width = 0;
            $new_height = 0;

            //Calculations
            if ($requested_width > $requested_height) {
                $new_width = $requested_width;
                $new_height = $new_width / $ratio;
                if ($requested_height == 0)
                    $requested_height = $new_height;

                if ($new_height < $requested_height) {
                    $new_height = $requested_height;
                    $new_width = $new_height * $ratio;
                }
            } else {
                $new_height = $requested_height;
                $new_width = $new_height * $ratio;
                if ($requested_width == 0)
                    $requested_width = $new_width;

                if ($new_width < $requested_width) {
                    $new_width = $requested_width;
                    $new_height = $new_width / $ratio;
                }
            }

            $new_width = ceil($new_width);
            $new_height = ceil($new_height);

            //Resizing
            $config = array();
            $config['image_library'] = 'gd2';
            $config['source_image'] = $image_path;
            $config['new_image'] = $new_image_path;
            $config['maintain_ratio'] = FALSE;
            $config['height'] = $new_height;
            $config['width'] = $new_width;
            $CI->image_lib->initialize($config);
            $CI->image_lib->resize();
            $CI->image_lib->clear();
            //Crop if both width and height are not zero
            if (($width != 0) && ($height != 0)) {
                $x_axis = floor(($new_width - $width) / 2);
                $y_axis = floor(($new_height - $height) / 2);

                //Cropping
                $config = array();
                $config['source_image'] = $new_image_path;
                $config['maintain_ratio'] = FALSE;
                $config['new_image'] = $new_image_path;
                $config['width'] = $width;
                $config['height'] = $height;
                $config['x_axis'] = $x_axis;
                $config['y_axis'] = $y_axis;
                $config['quality'] = '100%';
                $CI->image_lib->initialize($config);
                $CI->image_lib->crop();
                $CI->image_lib->clear();
            }
        }
        return PATH_URL . $new_image_path;
    }

}

function DaysOfMonth($mon, $y) {
    switch ($mon) {
        case 2:
            if (($y % 4 == 0) && ($y % 400 != 0))
                return 29;
            else
                return 28;
            break;
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            return 31;
            break;
        default:
            return 30;
    }
}

function _token() {
    $token = md5(uniqid(rand(), TRUE)) . md5(uniqid(rand()) . time());
    return $token;
}
function url_search($string) {
    $entities = array('!', '#', '%', '^', '*', '(', ')', '[', ']', '@', '<', '>', '/', '|', '-', '$', '~', '=','+++','++','&',"'");
    $replacements = array( '', "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "","","","","");
    return str_replace($entities, $replacements, $string);
}
function timthumb($path, $w = '', $h = '',$zc = 1) {
    $name = explode('/', $path);

    if (file_exists($path) && $name[count($name) - 1] != '') {
        if ($w != '' && $h != '') {
            return TIMTHUMB . PATH_URL . $path . "&amp;w=$w&amp;h=$h&amp;zc=$zc&amp;q=100";
        } else if ($w != '' && $h == '') {
            return TIMTHUMB . PATH_URL . $path . "&amp;w=$w&amp;zc=$zc&amp;q=100";
        } else {
            return TIMTHUMB . PATH_URL . $path;
        }
    } else {
        $image_path = 'static/images/img/no-image.jpg';
        return TIMTHUMB . PATH_URL . $image_path . "&amp;w=$w&amp;h=$h&amp;zc=$zc&amp;q=100";
    }
}
?>